package com.deparse.dpframe.ui.activity;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.deparse.dpframe.R;
import com.deparse.dpframe.common.DPConsts;
import com.deparse.dpframe.ui.widget.DPCircleIndicator;
import com.deparse.dpframe.ui.widget.DPViewPager;
import com.deparse.dpframe.ui.widget.photoview.PhotoView;
import com.deparse.dpframe.ui.widget.photoview.PhotoViewAttacher;
import com.deparse.dpframe.utils.DPPicassoUtil;

import java.util.ArrayList;

/**
 * Created by MartinKent on 2016/8/11.
 */
public class DPImagePreviewActivity extends DPBaseActivity {
    protected DPViewPager viewPager;
    protected DPCircleIndicator indicator;

    private ArrayList<String> paths;
    private int selected;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.dp_image_preview_activity);
        initView();
    }

    private void initView() {
        viewPager = (DPViewPager) findViewById(R.id.view_pager);
        indicator = (DPCircleIndicator) findViewById(R.id.indicator);
        if (null != getIntent()) {
            if (getIntent().hasExtra(DPConsts.IntentKeys.dp_image_preview_activity_source)) {
                paths = getIntent().getStringArrayListExtra(DPConsts.IntentKeys.dp_image_preview_activity_source);
                selected = getIntent().getIntExtra(DPConsts.IntentKeys.dp_image_preview_activity_selected_pos, 0);

                MyAdapter adapter = new MyAdapter(getSupportFragmentManager(), this, paths, selected);
                viewPager.setAdapter(adapter);
                indicator.setViewPager(viewPager);
                viewPager.setCurrentItem(selected);
            }
        }
    }

    private static class MyAdapter extends FragmentPagerAdapter {

        private final DPImagePreviewActivity activity;
        private final ArrayList<String> data;
        private final int selected;

        public MyAdapter(FragmentManager fm, DPImagePreviewActivity activity, ArrayList<String> data, int selected) {
            super(fm);
            this.activity = activity;
            this.data = data;
            this.selected = selected;
        }

        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public Fragment getItem(int position) {
            MyFragment fragment = new MyFragment();
            fragment.setActivity(activity);
            fragment.setPath(data.get(position));
            return fragment;
        }
    }

    public static class MyFragment extends Fragment {
        private DPImagePreviewActivity activity;
        private String path;

        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            PhotoView photoView = new PhotoView(activity);
            DPPicassoUtil.with(activity).load(path).into(photoView);
            photoView.setOnPhotoTapListener(new PhotoViewAttacher.OnPhotoTapListener() {
                @Override
                public void onPhotoTap(View view, float x, float y) {
                    activity.finish();
                }

                @Override
                public void onOutsidePhotoTap() {
                    activity.finish();
                }
            });
            return photoView;
        }

        public void setActivity(DPImagePreviewActivity activity) {
            this.activity = activity;
        }

        public void setPath(String path) {
            this.path = path;
        }
    }
}
